package com.wcgksoft.controller.question;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.goods.Coupon;
import com.wcgksoft.entity.order.Order;
import com.wcgksoft.entity.question.QType;
import com.wcgksoft.service.question.FindQuestionService;
import com.wcgksoft.service.question.QTypeService;
import javafx.scene.chart.ValueAxis;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller
 * @Description TODO
 * @date 2020/5/24 18:58
 * @ClassName QuestionController
 */
@Controller
@RequestMapping("questionType")
public class QuestionController {
    @Autowired
    private QTypeService qTypeService;

    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit){
        DetachedCriteria criteria = DetachedCriteria.forClass(QType.class);
        List<QType> list = qTypeService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", qTypeService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "findAllType")
    public Layui findAllType(){
        DetachedCriteria criteria = DetachedCriteria.forClass(QType.class);
        List<QType> list = qTypeService.findByConditions(criteria);
        return Layui.data(0, "success", qTypeService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data",required = false)String data){
        JSONObject param = JSONObject.fromObject(data);
        QType qType=null;
        if (null==param){
            return Layui.data(1, "请求格式有误", 0, null);
        }else{
            qType=(QType) JSONObject.toBean(JSONObject.fromObject(data), QType.class);
            qType.setAddTime(new Date());
            qTypeService.save(qType);
            return Layui.data(0,"success",1,Layui.addList(qType));
        }
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id",required = false) Integer id){
        try {
            QType byId = qTypeService.findById(id);
            qTypeService.delete(byId);
            return Layui.data(0,"操作成功",0,null);
        }catch (Exception e){
            e.printStackTrace();
            return Layui.data(0,"删除失败",0,null);
        }

    }
}
